package com.eqdd.yiqidian.ui.gongneng.car;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CarListInfoBean;
import com.eqdd.yiqidian.bean.rebuild.EmploymentListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditChooseBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.SelectRichTextActivity;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;

/**
 * @author吕志豪 .
 * @date 17-11-20  上午11:36.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class AddAccidentActivity extends SlimBaseMultiEditActivity {

    private RecyclerViewCustom dataBinding;
    @ToSave
    RichTextResult richTextResult;
    private Bundle bundle;
    @ToSave
    CarListInfoBean carListInfoBean;
    private String msg;
    private EmploymentListResultBean employmentListResultBean;

    @Override
    public void initBinding() {
        /**
         theTime：事故发生时间
         thePlace：事故地点
         dutyRatio：责任比例
         cost：花费金额（元）
         */
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "添加事故记录");
        initTopRightText("确定", v -> {
            if (checkParams()) {
                PostRequest<HttpResult> params = OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_CAR_ACCIDENT)
                        .tag(AddAccidentActivity.this)
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("createrName", AppConstent.user.getUsername())
                        .params("vehicleId", carListInfoBean.getId())
                        .params("plateNumber", carListInfoBean.getPlateNumber())
                        .params("personLiableName", setParam(1))
                        .params("theTime", setParam(2))
                        .params("thePlace", setParam(3))
                        .params("dutyRatio", setParam(4))
                        .params("cost", setParam(5))
                        .params("remark", richTextResult == null ? " " : richTextResult.getContent());
                if (richTextResult != null) {
                    for (int i = 0; i < richTextResult.getSelectMedia().size(); i++) {
                        params.params("file" + i, new File(richTextResult.getSelectMedia().get(i).getCompressPath()));
                    }
                }
                params.execute(new DialogCallBack<HttpResult>(AddAccidentActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            clearData();
                            setResult(Config.SUCCESS);
                            finish();
                        }
                    }
                });
            } else {
                ToastUtil.showShort(msg);
            }

        });
    }

    @Override
    protected void onDestroy() {
        OkGo.getInstance().cancelTag(this);
        super.onDestroy();
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 0:
                bundle = new Bundle();
                bundle.putBoolean(Config.IS_SHOW_ALL, true);
                startActivityForResult(SelectCarActivity.class, bundle, Config.SELECT_CAR);
                break;
            case 1:
                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
                break;
            case 6:
                bundle = new Bundle();
                bundle.putString(Config.TITLE, "备注");
                bundle.putSerializable(Config.RICH_TEXT_RESULT, richTextResult);
                startActivityForResult(SelectRichTextActivity.class, bundle, Config.SELECT_RICH_TEXT);
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditEnterBean("*车", "", 0, true, "请选择").setNeed(true));
        datas.add(new SlimEditEnterBean("*责任人", "", 1, true, "请选择").setNeed(true));
        datas.add(new SlimEditTimeBean("*事故日期", "", 2, "请选择").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
        datas.add(new SlimEditEnterBean("*地点", "", 3, "请填写").setNeed(true));
        datas.add(new SlimEditEnterBean("*责任比例", "", 4, "请填写").setNeed(true));
        datas.add(new SlimEditEnterBean("*花费金额(元)", "", 5, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditEnterBean("备注", "", 6, true, "请输入"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.SELECT_RICH_TEXT:
                    richTextResult = (RichTextResult) data.getSerializableExtra(Config.RICH_TEXT_RESULT);
                    datas.get(6).setContent(richTextResult.getContent());
                    slimAdapterEx.notifyItemChanged(6);
                    break;
                case Config.SELECT_CAR:
                    carListInfoBean = (CarListInfoBean) data.getSerializableExtra(Config.BEAN);
                    slimAdapterEx.getDataItem(0).setContent(carListInfoBean.getVehicleType() + "-" + carListInfoBean.getPlateNumber());
                    slimAdapterEx.notifyItemChanged(0);
                    break;
            }
        } else if (requestCode == Config.SELECT_SINGLE_STAFF && resultCode == Config.INTENT_CONTENT) {
            employmentListResultBean = (EmploymentListResultBean) data.getSerializableExtra(Config.BEAN);
            slimAdapterEx.getDataItem(1).setContent(employmentListResultBean.getStaffName());
            slimAdapterEx.notifyItemChanged(1);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }

    }
}
